Skip to content

Exact adjoint for fw3d_mode=False, without forcing fw3d_mode (devito#2780) - #124

Merged
ccuetom merged 2 commits into
masterfrom
dev/adjoint-form-devito2780
Dec 2, 2026
Merged

ccuetom merged 2 commits into
masterfrom
dev/adjoint-form-devito2780

Conversation

@acdanila

@acdanila acdanila commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem

IsoAcousticDevito.before_adjoint forces fw3d_mode = True # TODO Force fw3d_mode pending Devito-side fix. That causes two problems:

  1. The forced adjoint is one step off. The forward records p.forward, but the fw3d-form adjoint is the exact transpose of recording p. A dot-product test of the source-to-receiver map (random broadband source and residual, 3 trials, spread = std/|mean| of <Fs,r>/<s,Fᵀr>) gives a spread of 1.9e-1 on master. An exact adjoint gives about 5e-5.
  2. fw3d_mode=False is ignored. The forcing was added for a Devito scheduling bug, devitocodes/devito#2780:
    • Trigger: a backward operator writes p_a[t] before the stencil (the old non-fw3d receiver injection), then an equation after the stencil reads p_a.backward. stride-private's local preconditioner (p_a.backward.dt) does this. stride's own gradient equations do not.
    • Before devito#3008: Devito moves the later equation into a second time loop that runs forward over the 3-slot p_a buffer.
    • After devito#3008 (main): Devito keeps one loop but runs the whole loop forward.

Also, a vp gradient computed together with a wavelet, rho or alpha gradient (the "joint" path) did not work:

  • _forward_save_undersampled passes bounds= to undersampled_time_derivative, which takes time_bounds=, so it raised a TypeError.
  • With that fixed, devitopro's code that streams p_saved still used the factor of p_saved's own time dimension, which undersampled_time_derivative had replaced, so it failed with InvalidArgument: No value found for parameter time_under0f.
  • With that fixed, the vp gradient was wrong at save_undersampling>1: p_dt is a Function with no time dimension, so its Inc ran at every time step with the last saved value.

Fix

The fixes apply to fw3d_mode=False, the default. fw3d_mode=True behaves as on master. This PR needs no Devito change.

  • Delete the forced line, so fw3d_mode is again the caller's choice (default False).
  • One adjoint layout for both modes: inject the receivers into p_a.backward after the stencil. Injecting rec(t) into p_a before the stencil gives the same sum, but it is the layout that triggers devito#2780. With this layout, Devito compiles one backward time loop, with or without the Devito bug.
  • Delay the adjoint source by one step when fw3d_mode=False, so the adjoint pairs with recording p.forward and is exact. fw3d_mode=True keeps Fullwave3D's pairing (no delay), as on master. shift_adjoint_source=True/False overrides the default in either mode.
  • Joint path:
    • bounds= → time_bounds= in _forward_save_undersampled.
    • undersampled_time_derivative keeps the function's own undersampled time dimension (a new one only with an offset; no caller passes one).
    • The joint vp gradient and preconditioner are added only at the saved time steps, as in the vp-only path.
    • The wavelet gradient does not read p_saved, so only a rho or alpha gradient (or force_raw_wavefield) saves the raw wavefield. A vp and wavelet gradient now saves the same derivative as a vp gradient alone, so its vp gradient is identical. prepare_grad_vp now also passes force_raw_wavefield, so it matches what the forward saved.

Behaviour change: with the default fw3d_mode=False, the adjoint was the forced fw3d form without a delay (one step off). It is now exact. fw3d_mode=True is unchanged.

stride-private's IsoAcousticDevito inherits before_adjoint, so it gets this layout too. Its own joint-path code is fixed in trustimaging/stride-private#92, which needs this PR.

Evidence

Small 3D problem, 1 A100, nvidia-cuda (sponge_boundary_2, hicks, OT4), Devito 4.8.22.dev142 without any devito#2780 fix. Every case compiles one backward adjoint time loop.

stride's IsoAcousticDevito:

dot-test spread FD/⟨g,dv⟩, 3 blobs + random (save_undersampling=8)
master (forced fw3d form, no delay) 1.9e-1 –
this PR, fw3d_mode=True (no delay, as master) 1.87e-1 –
this PR, fw3d_mode=False 5.46e-5 −0.01015 / −0.01040 / −0.01077 / −0.00788
this PR, fw3d_mode=False, save_undersampling=1 – −0.00125 / −0.00130 / −0.00134 / −0.00103

The FD ratio is constant up to stride's gradient scale and sign convention.

stride-private's IsoAcousticDevito (local preconditioner), save_undersampling=8:

FD/⟨g,dv⟩, 3 blobs + random
before this PR, old non-fw3d layout, Devito with the bug −766 (GPU), −2246 (CPU), blob centre: wrong
this PR, fw3d_mode=False, GPU −0.01508 / −0.01472 / −0.02761 / −0.02338
this PR, fw3d_mode=False, CPU −0.01508 / −0.01469 / −0.02759 / −0.02320
this PR, fw3d_mode=True + shift_adjoint_source=True, GPU same as fw3d_mode=False

These ratios are not constant because stride-private applies its local preconditioner to the gradient. GPU and CPU agree.

Joint path, raw grad_vp compared with the vp-only gradient (same setup):

save_undersampling=1 save_undersampling=8
master TypeError TypeError
vp + wavelet gradient, stride and stride-private identical (rel. diff 0) identical (rel. diff 0)
raw-wavefield path (force_raw_wavefield, as for rho), stride corr 0.999 FD ratios −0.0123 / −0.0124 / −0.0130 / −0.0110
same, stride-private (with stride-private#92) corr 0.999 corr 0.92 (see below)

At save_undersampling=1, the raw-wavefield path differs from the vp-only path by as much as GPU and CPU runs of the same code differ from each other (corr 0.999). At save_undersampling=8, it takes time derivatives from samples 8 steps apart, which is less accurate than saving the derivative. Against the save_undersampling=1 gradient, stride's raw-wavefield path is more accurate (corr 0.954) than the vp-only path (0.923). stride-private's first derivative is less accurate (0.847 vs 0.933). The wavelet dot test with stride-private gives a spread of 5.55e-5.

Production check (earlier layout)

Before this layout, the exact non-fw3d adjoint injected rec(t) into p_a before the stencil and needed a Devito fix for stride-private. With that fix, a full reg004 iteration (stride-private, 5420 shots, 8 A100s), non-fw3d form against the fw3d-form reference run:

field corr rel. diff scale
raw vp gradient 0.99984 1.75e-2 1.0003
raw vp preconditioner 0.99993 4.1e-3 1.0001
processed vp gradient 0.99987 1.65e-2 1.0000
updated vp 0.99971 2.39e-2 0.9991

That difference came from stride-private's vp gradient, which reads p_a.backward.dt: in the old non-fw3d layout, p_a[t-1] did not yet hold the current injection. This PR's layout is the fw3d layout with a one-step delay, so stride-private's gradient with fw3d_mode=False now matches the delayed fw3d form exactly (table above).

Devito side

devito#2780 is still open. devitocodes/devito#3132 proposes a fix. This PR does not depend on it.

🤖 Generated with Claude Code

@acdanila
acdanila force-pushed the dev/adjoint-form-devito2780 branch from a63ddc5 to bfb5e5a Compare November 27, 2026 23:48
@acdanila acdanila changed the title Restore the exact non-fw3d adjoint, with a guard for the Devito schedule bug (devito#2780) Restore the exact non-fw3d adjoint and fix the fw3d one-step offset (devito#2780) Nov 27, 2026
@acdanila acdanila changed the title Restore the exact non-fw3d adjoint and fix the fw3d one-step offset (devito#2780) Restore the exact non-fw3d adjoint for fw3d_mode=False (devito#2780) Dec 1, 2026
@acdanila acdanila changed the title Restore the exact non-fw3d adjoint for fw3d_mode=False (devito#2780) Exact adjoint for fw3d_mode=False, without forcing fw3d_mode (devito#2780) Dec 1, 2026
before_adjoint forced fw3d_mode = True as a workaround for devitocodes/devito#2780,
so fw3d_mode=False was ignored and the adjoint was one step off the forward, which
records p.forward.

- Remove the forced fw3d_mode, so the default fw3d_mode=False applies again.
- Inject the receivers into p_a.backward after the stencil in both modes. Injecting
  into p_a before the stencil gives the same sum, but Devito then compiles a wrong
  time loop when a later equation reads p_a.backward (devito#2780). This layout
  compiles one backward time loop with any Devito version.
- Delay the adjoint source by one step when fw3d_mode=False, which makes the adjoint
  exact. fw3d_mode=True keeps Fullwave3D's pairing, as before.
  shift_adjoint_source overrides the default in either mode.

Also fix the vp gradient computed together with a wavelet, rho or alpha gradient:

- Pass time_bounds= (not bounds=) to undersampled_time_derivative.
- Keep the function's own undersampled time dimension in
  undersampled_time_derivative, so the code that streams p_saved has a value for
  its factor.
- Add the vp gradient and preconditioner only at the saved time steps.
- Save the raw wavefield only for a rho or alpha gradient (or force_raw_wavefield).
  The wavelet gradient does not read it, so a vp and wavelet gradient now gives
  the same vp gradient as a vp gradient alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@acdanila
acdanila force-pushed the dev/adjoint-form-devito2780 branch from 1300f44 to ac8325d Compare December 1, 2026 17:51
@acdanila
acdanila requested a review from ccuetom December 1, 2026 17:51
pass

if self._needs_grad(wavelets, rho, alpha, **kwargs):
if self._needs_grad(rho, alpha, **kwargs):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is wavelets removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude reply:

p_saved is only used by the rho (and alpha) gradient, which needs p itself. The wavelet gradient doesn't read it: prepare_grad_wavelets returns () and the gradient comes from src.interpolate(-p_a) in the adjoint. So with wavelets in the check, a vp + wavelet inversion saved raw p for nothing, and the vp gradient had to rebuild p.dt2 from samples undersampling steps apart. That path was broken (bounds= TypeError, then No value found for parameter time_under0f from the streaming code), and once fixed it is less accurate than saving p.dt2 directly (corr 0.954 vs the full-rate gradient for stride, 0.847 for stride-private's first derivative).

Without wavelets in the check, vp + wavelet saves the same derivative as vp alone and gives a bit-identical vp gradient (rel. diff 0 at save_undersampling 1 and 8, stride and stride-private). The wavelet dot test is unchanged (spread 5.5e-5).

The second line also passes **kwargs now, so force_raw_wavefield is seen by both the forward and prepare_grad_vp. Before, force_raw_wavefield=True with a vp-only gradient saved raw p in the forward but used it as p.dt2 in the adjoint.

If you'd rather keep raw p whenever wavelets are inverted, I can put wavelets back; the joint path now works and is exact at save_undersampling=1.


wavelets, _, rho, alpha = kwargs.get('wrt')
if self._needs_grad(wavelets, rho, alpha):
if self._needs_grad(rho, alpha, **kwargs):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

@ccuetom
ccuetom merged commit 595c8f1 into master Dec 2, 2026
3 checks passed
@ccuetom
ccuetom deleted the dev/adjoint-form-devito2780 branch December 2, 2026 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants